package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.AssetsClass;
import com.zyd.shiro.business.service.PakAssetsClassService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakAssetsClass;
import com.zyd.shiro.persistence.mapper.PakAssetsClassMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakAssetsClassServiceImpl implements PakAssetsClassService {

    @Autowired
    private PakAssetsClassMapper pakAssetsClassMapper;

    @Override
    public AssetsClass insert(AssetsClass assetsClass) {
        Assert.notNull(assetsClass, "对象不可为空！");
        assetsClass.getPakAssetsClass().setUpdateTime(new Date());
        assetsClass.getPakAssetsClass().setCreateTime(new Date());
        pakAssetsClassMapper.insertSelective(assetsClass.getPakAssetsClass());
        return assetsClass;
    }

    @Override
    public void insertList(List<AssetsClass> assetsClasss) {
            for(AssetsClass assetsClass:assetsClasss){
               assetsClass.getPakAssetsClass().setUpdateTime(new Date());
               assetsClass.getPakAssetsClass().setCreateTime(new Date());
               pakAssetsClassMapper.insertSelective(assetsClass.getPakAssetsClass());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakAssetsClassMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AssetsClass assetsClass) {
        Assert.notNull(assetsClass, "对象不可为空！");
        assetsClass.setUpdateTime(new Date());
        return pakAssetsClassMapper.updateByPrimaryKey(assetsClass.getPakAssetsClass()) > 0;
    }

    @Override
    public boolean updateSelective(AssetsClass assetsClass) {
        Assert.notNull(assetsClass, "对象不可为空！");
        assetsClass.setUpdateTime(new Date());
        return pakAssetsClassMapper.updateByPrimaryKeySelective(assetsClass.getPakAssetsClass()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public AssetsClass getByPrimaryKey(Long primaryKey) {
        PakAssetsClass pakAssetsClass = pakAssetsClassMapper.selectByPrimaryKey(primaryKey);
        return null == pakAssetsClass ? null : new AssetsClass(pakAssetsClass);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public AssetsClass getOneByEntity(AssetsClass test) {
        Assert.notNull(test, "对象不可为空！");
        PakAssetsClass pakAssetsClass = pakAssetsClassMapper.selectOne(test.getPakAssetsClass());
        return null == pakAssetsClass ? null : new AssetsClass(pakAssetsClass);
    }

    @Override
    public List<AssetsClass> listAll() {
        List<PakAssetsClass> pakAssetsClasss = pakAssetsClassMapper.selectAll();
        if (CollectionUtils.isEmpty(pakAssetsClasss)) {
            return null;
        }
        List<AssetsClass> assetsClasss = new ArrayList<>();
        for (PakAssetsClass pakAssetsClass : pakAssetsClasss) {
            assetsClasss.add(new AssetsClass(pakAssetsClass));
        }
        return assetsClasss;
    }

    @Override
    public List<AssetsClass> listByEntity(AssetsClass entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<AssetsClass> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakAssetsClass> pakAssetsClasss = pakAssetsClassMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakAssetsClasss)) {
            return null;
        }
        List<AssetsClass> assetsClasss = new ArrayList<>();
        for (PakAssetsClass pakAssetsClass : pakAssetsClasss) {
            assetsClasss.add(new AssetsClass(pakAssetsClass));
        }
        PageInfo bean = new PageInfo<PakAssetsClass>(pakAssetsClasss);
        bean.setList(assetsClasss);
        return bean;
    }
@Override
    public void increase(PakAssetsClass pakAssetsClass) {
        pakAssetsClassMapper.insert(pakAssetsClass);
    }

    @Override
    public Long findByName(String name) {
        return pakAssetsClassMapper.findIdByName(name);
    }


}
